/**
 * 
 */
package com.p2p.base.model;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.DecimalFormat;

import com.p2p.base.util.DoubleUtil;



/**
 * @author ruiqi
 *
 */
public class Money implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -1389753745804061815L;

	long cent;	
	
	public Money(long integerPart,int DecimalPart){
		this.cent =integerPart*100L+DecimalPart;
	}

	public Money(Long cent){
		this.cent = cent;
	}
	/*public Money(Double amount){

		BigDecimal bd = new BigDecimal(amount);

		String strAmount = bd.toString();
		
		String strIntegerPart="";
		String decimalPartStr= "";
		int index = strAmount.indexOf(".");
		if(index==-1){
			strIntegerPart = strAmount;
			decimalPartStr = "0";
		}else{
			strIntegerPart = strAmount.substring(0,index);
			decimalPartStr = strAmount.substring(index+1);
			if(decimalPartStr.length()>2){
				decimalPartStr = decimalPartStr.substring(0, 2);
			}else if(decimalPartStr.length()==1){
				decimalPartStr =decimalPartStr+"0";
			}
		}
		
		
		
		this.cent=Long.valueOf(strIntegerPart)*100;
		if(cent>=0){
			this.cent=cent+Long.valueOf(decimalPartStr);
		}else{
			this.cent=cent-Long.valueOf(decimalPartStr);
		}
	}*/
	public Money(Double amount){
		Double d = DoubleUtil.mul(amount, 100);
		this.cent = d.longValue();
	}
	public Money(){
		
	}
	public long getCent() {
		return cent;
	}

	public void setCent(long cent) {
		this.cent = cent;
	}
	
/*	public long getIntegerPart() {
		return cent/100;
	}*/
	public void setYuan(Double a){
		long temp = new Money(a).cent;
		this.cent= temp;
	}
	public Double doubleValueYuan(){
		long integerPart = cent/100;
		long decimalPart= cent%100;
		String decimalPartStr = String.format("%02d", Math.abs(decimalPart));
		return Double.valueOf(integerPart+"."+decimalPartStr);
	}
	/*public String getYuan(){
		//BigDecimal bd = new BigDecimal(new Long(cent).doubleValue()/100).setScale(2,BigDecimal.ROUND_UP);
		long integerPart = cent/100;
		long decimalPart= cent%100;
		String decimalPartStr = String.format("%02d", Math.abs(decimalPart));
		//String doubleStr = String.valueOf(integerPart);
		if(decimalPart>0){
			doubleStr=
		}
		return new BigDecimal(integerPart+"."+decimalPartStr).toString();
		//return Double.valueOf(integerPart+"."+decimalPartStr);
	}*/
	public Double getYuan(){
		//BigDecimal bd = new BigDecimal(new Long(cent).doubleValue()/100).setScale(2,BigDecimal.ROUND_UP);
		long integerPart = cent/100;
		long decimalPart= cent%100;
		String decimalPartStr = String.format("%02d", Math.abs(decimalPart));
		//String doubleStr = String.valueOf(integerPart);
		/*if(decimalPart>0){
			doubleStr=
		}*/
		return new BigDecimal(integerPart+"."+decimalPartStr).doubleValue();
		//return Double.valueOf(integerPart+"."+decimalPartStr);
	}
	/**
	 * 这里有个坑，这里的setIntPart是和上面的getIntegerPart对应的，但是由于mvc参数绑定的问题，和webservice序列化的问题，会导致cent属性被修改
	 * @param integerPart
	 */
	/*public void setIntPart(long intPart) {
		this.cent = intPart*100;
	}*/
	
	
	public int getDecimalPart() {
		return (int)(cent-(cent/100*100));
	}

	public Money add(Money addMoney){
		this.cent=this.cent+addMoney.getCent();
		return this;
	}
	public Money sub(Money addMoney){
		this.cent=this.cent-addMoney.getCent();
		return this;
	}
	
	public Money createNewAmount(){
		return new Money(cent);
	}
	
	public void multiplyBy(int x){
		this.cent=cent*x;
	}
	
	@Override
	public String toString() {
		Double number = new Long(this.cent).doubleValue()/100;
		String strMoney = new DecimalFormat(",###.##").format(number);
		return strMoney;
	}
	
	public static void main(String[] args) {
		Double d1 = new Double("1045189788");
		
		//double d2 = 5.10l;
		//DoubleUtil.divideAndRemainder(v1, v2)
	}
	
	
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Money){
			if(((Money)obj).cent == this.cent){
				return true;
			}
		}
		return false;
	}
}
